package com.zivwu.library.utils;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.TypedValue;


public class DisplayUtil {

    /**
     * 将px值转换为dip或dp值，保证尺寸大小不变
     */
    public static float px2dip(Context context, float pxValue) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX,
                pxValue,
                context.getResources().getDisplayMetrics());


    }

    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     */
    public static float dip2px(Context context, float dipValue) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dipValue,
                context.getResources().getDisplayMetrics());
    }


    /**
     * SP转px
     * @param context   上下文
     * @param sp    text size
     * @return  px
     */
    protected float sp2px(Context context, int sp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics());
    }


    public static Drawable tintDrawable(Drawable drawable, ColorStateList colorStateList) {
        Drawable wrappedDrawable = DrawableCompat.wrap(drawable.mutate());
        DrawableCompat.setTintList(wrappedDrawable, colorStateList);
        return wrappedDrawable;
    }


}
